;*************************************************************************
; Title:	RMSave
; Author:	Philip Ludlam
; Copyright:	(C) Philip Ludlam 2006
; Version:	1.02, 7 May 2006
;
; Changes:	Updated for the A9home by Philip Ludlam
;
;*************************************************************************
; This program is free software; you can redistribute it and/or modify it
; under the terms of the GNU General Public License as published by the Free
; Software Foundation; either version 2 of the License, or (at your option)
; any later version.
;
; This program is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR APARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; this program; if not, write to the Free Software Foundation, Inc., 59 Temple
; Place - Suite 330, Boston, MA 02111-1307, USA
;
;*************************************************************************


		TTL	> RMSave

		GET	OSLib:oslib.hdr.types
		GET	OSLib:oslib.hdr.OS
		GET	OSLib:oslib.hdr.OSModule
		GET	OSLib:oslib.hdr.OSFile
		GET	AsmLib2:hdr.RegsBoth

		AREA	|main|, CODE, READONLY

		ENTRY

;-------------------------------------------------------------------------------

Start		B	Entry			; Entry instruction
		DCD	&79766748		; Magic value 1
		DCD	&216C6776		; Magic value 2
		DCD	End-Start		; Read only size
		DCD	0			; Read/write size
		DCD	32			; 26 or 32 bit build

Entry		ADR	r0, syntax_string
		MOV	r2, r12			; r2 -> workspace
		SUB	r3, r13, r12		; r3 = workspace size
		SWI	XOS_ReadArgs		; r1 already -> arguments
		BVS	syntax_code_		; report error

		LDR	r3, [r12, #0]		; if the user did not supply
		CMP	r3, #0			; any arguments
		BEQ	syntax_code		; tell them.

		MOV	r0, #OSModule_Lookup	; r0 = 18
		LDR	r1, [r12]		; r1 -> module name
		SWI	XOS_Module
		BVS	nomodule_code		; error if can't find module

		MOV	r0, #OSFile_SaveStamped	; r0 = 10
		LDR	r1, [r12, #4]		; r1 -> filename
		CMP	r1, #0
		LDREQ	r1, [r12, #0]		; r1 -> filename
		LDR	r2, =&FFA		; r2 = &FFA
		LDR	r5, [r3, #-4]
		MOV	r4, r3
		ADD	r5, r4, r5
		MOV	r3, #0
		SWI	XOS_File
		MOVVC	pc, lr			; exit if no error

		ADR	r0, nosave_help
		MOV	pc, lr

syntax_code	SetV
syntax_code_	ADR	r0, syntax_help
		MOV	pc, lr

nomodule_code	ADR	r0, nomodule_help
		MOV	pc, lr

		LTORG

syntax_help	DCD	0
		DCB	"RMSave: <module> [<filename>]", 0
		ALIGN

nomodule_help	DCD	0
		DCB	"RMSave: can't find module", 0
		ALIGN

nosave_help	DCD	0
		DCB	"RMSave: can't save module", 0

syntax_string	DCB	",", 0

licence		DCB	" PJL, 2006. Licence: GPL."
		ALIGN

End
		END
